package cn.wolfcode.p2p.website.controller;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.base.util.JSONResult;
import cn.wolfcode.p2p.base.util.UserContext;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import cn.wolfcode.p2p.business.service.IBidService;
import cn.wolfcode.p2p.util.RequireLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

@Controller
public class BorrowController {

    @Autowired
    private IAccountService accountService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IBidRequestService bidRequestService;
    @Autowired
    private IRealAuthService realAuthService;


    @RequestMapping("/borrow")
    public String borrowPage(Model model){

        if (UserContext.getCurrent() == null) {
            return "redirect:borrowIndex.html";
        } else {
            model.addAttribute("account", accountService.getCurrent());
            model.addAttribute("userInfo", userInfoService.getCurrent());
            return "borrow";
        }
    }

    @RequestMapping("/borrowInfo")
    @RequireLogin
    public String borrowInfoPage(Model model){
        UserInfo current = userInfoService.getCurrent();
        if (!current.getCanBorrow()) { //如果不满足借款条件
            return "redirect:/borrow";
        }
        //如果当前用户有正在申请的借款,返回结果界面
        if (current.getHasBidRequestProcess()) {
            return "borrow_apply_result";
        }
        //minBidRequestAmount
        model.addAttribute("minBidRequestAmount", Constants.BORROW_AMOUNT_MIN);
        //account
        model.addAttribute("account", accountService.getCurrent());
        //minBidAmount
        model.addAttribute("minBidAmount",Constants.BID_AMOUNT_MIN);

        return "borrow_apply";
    }

    @RequestMapping("/borrow_apply")
    @ResponseBody
    public JSONResult apply(BidRequest bidRequest){
        JSONResult result = null;
        try {
            bidRequestService.apply(bidRequest);
            result = new JSONResult("");
        } catch (DisplayableException ex) {
            //异常需要在前台显示
            ex.printStackTrace();
            result = new JSONResult(false, ex.getMessage());
        } catch (Exception e){
            //异常记录到数据库或日志文件中
            e.printStackTrace();
            result = new JSONResult(false, "系统异常,请联系管理员");
        }
        return result;
    }

    @RequestMapping("/borrow_info")
    public String bidPage(Long id, Model model){
        //bidRequest
        BidRequest bidRequest = bidRequestService.get(id);
        model.addAttribute("bidRequest",bidRequest);
        //userInfo 借款人
        UserInfo createUserUserInfo = userInfoService.get(bidRequest.getCreateUser().getId());
        model.addAttribute("userInfo",createUserUserInfo);
        //realAuth 借款人
        model.addAttribute("realAuth", realAuthService.get(createUserUserInfo.getRealAuthId()));
        //self  是否是借款人账号
        if (UserContext.getCurrent() != null) {
            if (!UserContext.getCurrent().getId().equals(bidRequest.getCreateUser().getId())) {
                model.addAttribute("self", false);
                //account 当前用户的账户信息
                model.addAttribute("account", accountService.getCurrent());
            } else {
                //自己人
                model.addAttribute("self", true);
            }
        } else {
            //登录后投标
            model.addAttribute("self", false);
        }
        return "borrow_info";
    }

    @RequestMapping("/borrow_bid")
    @ResponseBody
    public JSONResult bid(Long bidRequestId, BigDecimal amount){
        JSONResult result = null;
        try {
            bidRequestService.bid(bidRequestId, amount);
            result = new JSONResult("");
        } catch (DisplayableException ex) {
            //异常需要在前台显示
            ex.printStackTrace();
            result = new JSONResult(false, ex.getMessage());
        } catch (Exception e){
            //异常记录到数据库或日志文件中
            e.printStackTrace();
            result = new JSONResult(false, "系统异常,请联系管理员");
        }
        return result;
    }

    @RequestMapping("/cancelBorrow")
    @ResponseBody
    public JSONResult cancelBorrow(Long bidRequestId){
        JSONResult result = null;
        try {
            bidRequestService.cancelBorrow(bidRequestId);
            result = new JSONResult("");
        } catch (DisplayableException ex) {
            //异常需要在前台显示
            ex.printStackTrace();
            result = new JSONResult(false, ex.getMessage());
        } catch (Exception e){
            //异常记录到数据库或日志文件中
            e.printStackTrace();
            result = new JSONResult(false, "系统异常,请联系管理员");
        }
        return result;
    }
}
